/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2016 pjoc.pub, blademainer.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */

package pub.pjoc.pay.vo;

import com.fasterxml.jackson.annotation.JsonIgnore;
import pub.pjoc.pay.constants.ResultCodeConstants;

import java.io.Serializable;

/**
 * @author xiongyingqi
 * @since 16-5-24 下午5:39
 */
public class ReturnResult implements Serializable, ResultCodeConstants {
  /**
   * 成功
   */
  public static final ReturnResult SUCCESS = new ReturnResult(ResultCodeConstants.CODE_SUCCESS,
      "SUCCESS", "成功");
  /**
   * 系统错误
   */
  public static final ReturnResult SYSTEM_ERROR = new ReturnResult(ResultCodeConstants.CODE_SYSTEM_ERROR,
      "SYSTEM_ERROR", "系统错误");
  /**
   * 验证签名错误
   */
  public static final ReturnResult CHECK_SIGN_ERROR = new ReturnResult(ResultCodeConstants.CODE_CHECK_SIGN_ERROR,
      "CHECK_SIGN_ERROR", "验证签名失败！");
  /**
   * 验证码已过期
   */
  public static final ReturnResult VERIFY_CODE_EXPIRED = new ReturnResult(ResultCodeConstants.CODE_VERIFY_CODE_EXPIRED,
      "VERIFY_CODE_EXPIRED", "验证码失效");
  /**
   * 验证码错误
   */
  public static final ReturnResult VERIFY_CODE_ERROR = new ReturnResult(ResultCodeConstants.CODE_VERIFY_CODE_ERROR,
      "VERIFY_CODE_ERROR", "验证码错误");

  private Integer code;
  private String message;
  private String describe;

  public ReturnResult(Integer code, String message, String describe) {
    this.code = code;
    this.message = message;
    this.describe = describe;
  }

  public ReturnResult() {
  }

  public Integer getCode() {
    return code;
  }

  public void setCode(Integer code) {
    this.code = code;
  }

  public String getMessage() {
    return message;
  }

  public void setMessage(String message) {
    this.message = message;
  }

  public String getDescribe() {
    return describe;
  }

  public void setDescribe(String describe) {
    this.describe = describe;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (!(o instanceof ReturnResult)) {
      return false;
    }

    ReturnResult that = (ReturnResult) o;

    return code != null ? code.equals(that.code) : that.code == null;

  }

  @Override
  public int hashCode() {
    return code != null ? code.hashCode() : 0;
  }

  @Override
  public String toString() {
    return "ReturnResult{" +
        "code=" + code +
        ", message='" + message + '\'' +
        ", describe='" + describe + '\'' +
        '}';
  }

  @JsonIgnore
  public boolean isSuccess() {
    return isSuccess(this);
  }

  public static boolean isSuccess(int code) {
    return code == SUCCESS.code;
  }

  public static boolean isSuccess(ReturnResult resultCode) {
    return resultCode != null && SUCCESS.code.equals(resultCode.code);
  }

}
